var uid, ws;
document.addEventListener('plusready', function(){
	ws = plus.webview.currentWebview();
	uid = plus.storage.getItem('uid');
	console.log(uid);
	showWating(); 
	console.log(apiRoot+"/home/medicinal/cartList/uid/"+uid)
	$.ajax({
		type :"get",
		url :apiRoot+"/home/medicinal/cartList",
		data :{uid:uid},
		dataType :'json',
		success:function(data){
//			console.log(JSON.stringify(data));
			plus.nativeUI.closeWaiting();
			if(data.length > 0){
				var list = '';
				$.each(data, function(k, v) {
					list += '<ul class="mui-table-view boot-one delgood'+v.id+'">'+
				        '<li class="mui-table-view-cell mui-media">'+
				        	'<div class="mui-input-row mui-checkbox mui-left">'+
								'<input name="checkbox" type="checkbox" cart="'+v.id+'" title="'+v.title+'" pic="'+v.pic+'" value="'+v.id+'" pid="'+v.pid+'" data-price="'+v.price+'" hid="'+v.hospital+'">'+
							'</div>'+
				            '<a href="javascript:;">'+
				                '<img class="mui-media-object mui-pull-left" src="'+getAvatar(v.pic)+'">'+
				                '<div class="mui-media-body">'+
				                    '<span>'+v.title+'</span>'+
				                    '<span class="span-img mui-pull-right" id="'+v.id+'">'+
				                    	'<img src="../images/shanc.png"/>'+
				                    '</span>'+
				                    '<p class="mui-ellipsis first-p"><span>¥'+v.price+'/g</span> </p>'+
				                    '<div class="mui-numbox" data-numbox-min=1 data-numbox-max=100>'+
										'<button class="mui-btn mui-btn-numbox-minus" type="button">-</button>'+
										'<input class="mui-input-numbox" type="number" id="num'+v.pid+'" value="'+v.num+'" />'+
										'<button class="mui-btn mui-btn-numbox-plus" type="button">+</button>'+
									'</div>'+
				                '</div>'+
				            '</a>'+
				        '</li>'+
				    '</ul>';
				});
				$('#content').html(list);
				clickEvent();
				$('.span-img').on('tap', function(){
					var id = $(this).attr('id');
					plus.nativeUI.confirm('确定删除该商品吗', function(e) {
						if(e.index === 0){
							delGoods(id);
						}
					}, null, ['确定', '取消'])
				})
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	
	//完善订单
	$('.payGoods').on('tap',function(){
		//获取选中的商品id、商品价格price、商品数量num、商品标题、缩略图、医馆id、购物车id
		var arrId = price = nums = title = pic = hid = cart = ""; 
		$("#content input[type='checkbox']:checked").each(function(){
			arrId += $(this).attr('pid') + ",";
			price += $(this).attr('data-price')+ ',';
			nums += $('#num'+$(this).attr('pid')).val() + ',';
			title += $(this).attr('title') + ';';
			pic += $(this).attr('pic') + ';';
			hid += $(this).attr('hid') + ',';
			cart += $(this).attr('cart') + ',';
		})
		var str = $('.zongji').text();	//总计
		var zong = str.substr(1, str.length);
		var totalgoods = $('.totalgoods').text();	//共计多少商品
		var total = totalgoods.replace('共计商品：', '');
//		console.log(arrId+" "+nums+" "+price+" "+zong+" "+total);return;
		if(arrId == ''){
			toast('未选中商品');
		}else{ 
//			toast('请完善订单信息');
			if(arrId != ''){
				goNewPage('shop-quer.html',{arrId:arrId, nums:nums, price:price, zong:zong, total:total, title:title, pic:pic, hid:hid, cart:cart, spage :ws.id})
			}
		}
	})
}, false)

var clickEvent=function(){
	//mui +- 数量 失效 重写
	$(".mui-numbox button ").on('tap',function(){
		var bThis=$(this).parent();
		var bmin=parseInt(bThis.attr('data-numbox-min'));//设置最小值
		var bmax=parseInt(bThis.attr('data-numbox-max'));//设置最大值
		var binput=parseInt(bThis.find("input").val());//当前数量
		if($(this).text()=='+'){
			binput++
			bThis.find("input").val(binput>bmax?bmax:binput);
			reckon();
		}else{
			binput--
			bThis.find("input").val((binput<bmin)?bmin:binput);
			reckon();
		}
	})
	//子复选框的事件  
	$("#content input[type='checkbox']").on('click', function() {
	    var chsub = $("#content input[type='checkbox']").length; //获取子复选框的个数  
	    var checkedsub = $("#content input[type='checkbox']:checked").length; //获取选中的子复选框个数  
	    if (checkedsub == chsub) { 
	        jQuery("#SelectAll").prop("checked",true);
	    }else{
			jQuery("#SelectAll").prop("checked",false);
	    }
		reckon();
	})
	
	//删除商品
	$('.deleteBtn').on('click',function(){
		//获取选中的购物车id
		var arrId=""; 
		$("#content input[type='checkbox']:checked").each(function(){
			arrId += $(this).val() + ",";   
		})
		if(arrId == ''){
			toast('未选中商品');return;
		}
		plus.nativeUI.confirm('确定删除该商品吗', function(e) {
			if(e.index === 0){
				delGoods(arrId);
			}
		}, null, ['确定', '取消'])
	})
	
}
//删除选中商品
function delGoods(arrId){
	showWating();
	console.log(apiRoot + "?m=home&c=medicinal&a=delCart&arrId=" + arrId);
	$.ajax({
		type:"get",   
		url:apiRoot + "?m=home&c=medicinal&a=delCart&arrId=" + arrId,
		success : function(data){
//			console.log(data);
			plus.nativeUI.closeWaiting();
			if(data == 0){
				toast('删除失败');
			}else{
				toast('删除成功');
				var strs= new Array(); //定义一数组 
				strs=arrId.split(","); //字符分割 
				for (i=0;i<strs.length ;i++ ){ 
					$('.delgood'+strs[i]).remove();
				} 
				$('.totalgoods').text('共计商品：0');
				$('.zongji').text('¥00.00');
				delSelect();
			}
		},
		error : function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
//删除选中状态
function delSelect(){
	jQuery("#SelectAll").prop("checked",false);
	jQuery("#content :checkbox").prop("checked",false);
}